package cn.tedu.ivos2.vehicle.service.impl;

import cn.tedu.ivos2.base.exception.DuplicateException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos2.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos2.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos2.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("查询车辆列表业务：vehicleQuery={}", vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        return list;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("保存车辆列表业务:vehicleSaveParam={}", vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam, vehicle);
        if(vehicle.getId()==null){
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            List<VehicleVO> list= vehicleMapper.selectVehicle(null);
            for(VehicleVO vehicleVO:list){
                if(vehicleVO.getLicense().equals(vehicle.getLicense())){
                    throw new DuplicateException(StatusCode.VEHICLE_DUPLICATE);
                }
                if(vehicleVO.getCode().equals(vehicle.getCode())){
                    throw new DuplicateException(StatusCode.VEHICLE_DUPLICATE);
                }
            }
            vehicleMapper.insert(vehicle);
        }else{
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("删除车辆业务:id={}", id);
        vehicleMapper.delete(id);
    }

    @Override
    public void unbind(Long id) {
        log.debug("解绑车辆业务:id={}", id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(null);
        vehicleMapper.unbind(vehicle);
    }

    @Override
    public void bind(Long id, Long geofenceId) {
        log.debug("绑定车辆业务:id={},geofenceId={}", id, geofenceId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(geofenceId);
        vehicleMapper.update(vehicle);
    }
}
